package arsmodeller.model.impl;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;

import java.util.Collection;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;
import org.eclipse.emf.ecore.util.EObjectWithInverseResolvingEList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.InternalEList;

import arsmodeller.model.Factory;
import arsmodeller.model.Link;
import arsmodeller.model.ObjectProcessDiagram;
import arsmodeller.model.ObjectProcessingDiagram;
import arsmodeller.model.Thing;
import arsmodeller.model.Package;

public class ThingImpl extends EObjectImpl implements Thing {
	/**
	 * The cached value of the '{@link #getIncomingLinks()
	 * <em>Incoming Links</em>}' reference list. <!-- begin-user-doc --> <!--
	 * end-user-doc -->
	 * 
	 * @see #getIncomingLinks()
	 * @generated
	 * @ordered
	 */
	protected EList<Link> incomingLinks;

	/**
	 * The cached value of the '{@link #getOutgoingLinks()
	 * <em>Outgoing Links</em>}' reference list. <!-- begin-user-doc --> <!--
	 * end-user-doc -->
	 * 
	 * @see #getOutgoingLinks()
	 * @generated
	 * @ordered
	 */
	protected EList<Link> outgoingLinks;

	/**
	 * The default value of the '{@link #getConstraints() <em>Constraints</em>}'
	 * attribute. <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @see #getConstraints()
	 * @generated
	 * @ordered
	 */
	protected static final Rectangle CONSTRAINTS_EDEFAULT = (Rectangle) Factory.eINSTANCE
			.createFromString(Package.eINSTANCE.getRectangle(), "");

	/**
	 * The cached value of the '{@link #getConstraints() <em>Constraints</em>}'
	 * attribute. <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @see #getConstraints()
	 * @generated
	 * @ordered
	 */
	protected Rectangle constraints = CONSTRAINTS_EDEFAULT;

	/**
	 * The default value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected static final String NAME_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected String name = NAME_EDEFAULT;

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	protected ThingImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return Package.Literals.OPM_THING;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public EList<Link> getIncomingLinks() {
		if (incomingLinks == null) {
			incomingLinks = new EObjectWithInverseResolvingEList<Link>(
					Link.class, this, Package.OPM_THING__INCOMING_LINKS,
					Package.OPM_LINK__TARGET);
		}
		return incomingLinks;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public EList<Link> getOutgoingLinks() {
		if (outgoingLinks == null) {
			outgoingLinks = new EObjectWithInverseResolvingEList<Link>(
					Link.class, this, Package.OPM_THING__OUTGOING_LINKS,
					Package.OPM_LINK__SOURCE);
		}
		return outgoingLinks;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public Rectangle getConstraints() {
		return constraints;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public void setConstraints(Rectangle newConstraints) {
		Rectangle oldConstraints = constraints;
		constraints = newConstraints;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET,
					Package.OPM_THING__CONSTRAINTS, oldConstraints, constraints));
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public String getName() {
		return name;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public void setName(String newName) {
		String oldName = name;
		name = newName;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET,
					Package.OPM_THING__NAME, oldName, name));
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public ObjectProcessingDiagram getOpd() {
		if (eContainerFeatureID() != Package.OPM_THING__OPD)
			return null;
		return (ObjectProcessingDiagram) eContainer();
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public NotificationChain basicSetOpd(ObjectProcessingDiagram newOpd,
			NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject) newOpd,
				Package.OPM_THING__OPD, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public void setOpd(ObjectProcessingDiagram newOpd) {
		if (newOpd != eInternalContainer()
				|| (eContainerFeatureID() != Package.OPM_THING__OPD && newOpd != null)) {
			if (EcoreUtil.isAncestor(this, newOpd))
				throw new IllegalArgumentException(
						"Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newOpd != null)
				msgs = ((InternalEObject) newOpd).eInverseAdd(this,
						Package.OPM_OBJECT_PROCESS_DIAGRAM__THINGS,
						ObjectProcessDiagram.class, msgs);
			msgs = basicSetOpd(newOpd, msgs);
			if (msgs != null)
				msgs.dispatch();
		} else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET,
					Package.OPM_THING__OPD, newOpd, newOpd));
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd,
			int featureID, NotificationChain msgs) {
		switch (featureID) {
		case Package.OPM_THING__INCOMING_LINKS:
			return ((InternalEList<InternalEObject>) (InternalEList<?>) getIncomingLinks())
					.basicAdd(otherEnd, msgs);
		case Package.OPM_THING__OUTGOING_LINKS:
			return ((InternalEList<InternalEObject>) (InternalEList<?>) getOutgoingLinks())
					.basicAdd(otherEnd, msgs);
		case Package.OPM_THING__OPD:
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			return basicSetOpd((ObjectProcessingDiagram) otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd,
			int featureID, NotificationChain msgs) {
		switch (featureID) {
		case Package.OPM_THING__INCOMING_LINKS:
			return ((InternalEList<?>) getIncomingLinks()).basicRemove(
					otherEnd, msgs);
		case Package.OPM_THING__OUTGOING_LINKS:
			return ((InternalEList<?>) getOutgoingLinks()).basicRemove(
					otherEnd, msgs);
		case Package.OPM_THING__OPD:
			return basicSetOpd(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public NotificationChain eBasicRemoveFromContainerFeature(
			NotificationChain msgs) {
		switch (eContainerFeatureID()) {
		case Package.OPM_THING__OPD:
			return eInternalContainer().eInverseRemove(this,
					Package.OPM_OBJECT_PROCESS_DIAGRAM__THINGS,
					ObjectProcessDiagram.class, msgs);
		}
		return super.eBasicRemoveFromContainerFeature(msgs);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
		case Package.OPM_THING__INCOMING_LINKS:
			return getIncomingLinks();
		case Package.OPM_THING__OUTGOING_LINKS:
			return getOutgoingLinks();
		case Package.OPM_THING__CONSTRAINTS:
			return getConstraints();
		case Package.OPM_THING__NAME:
			return getName();
		case Package.OPM_THING__OPD:
			return getOpd();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
		case Package.OPM_THING__INCOMING_LINKS:
			getIncomingLinks().clear();
			getIncomingLinks().addAll((Collection<? extends Link>) newValue);
			return;
		case Package.OPM_THING__OUTGOING_LINKS:
			getOutgoingLinks().clear();
			getOutgoingLinks().addAll((Collection<? extends Link>) newValue);
			return;
		case Package.OPM_THING__CONSTRAINTS:
			setConstraints((Rectangle) newValue);
			return;
		case Package.OPM_THING__NAME:
			setName((String) newValue);
			return;
		case Package.OPM_THING__OPD:
			setOpd((ObjectProcessingDiagram) newValue);
			return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
		case Package.OPM_THING__INCOMING_LINKS:
			getIncomingLinks().clear();
			return;
		case Package.OPM_THING__OUTGOING_LINKS:
			getOutgoingLinks().clear();
			return;
		case Package.OPM_THING__CONSTRAINTS:
			setConstraints(CONSTRAINTS_EDEFAULT);
			return;
		case Package.OPM_THING__NAME:
			setName(NAME_EDEFAULT);
			return;
		case Package.OPM_THING__OPD:
			setOpd((ObjectProcessingDiagram) null);
			return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
		case Package.OPM_THING__INCOMING_LINKS:
			return incomingLinks != null && !incomingLinks.isEmpty();
		case Package.OPM_THING__OUTGOING_LINKS:
			return outgoingLinks != null && !outgoingLinks.isEmpty();
		case Package.OPM_THING__CONSTRAINTS:
			return CONSTRAINTS_EDEFAULT == null ? constraints != null
					: !CONSTRAINTS_EDEFAULT.equals(constraints);
		case Package.OPM_THING__NAME:
			return NAME_EDEFAULT == null ? name != null : !NAME_EDEFAULT
					.equals(name);
		case Package.OPM_THING__OPD:
			return getOpd() != null;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy())
			return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (constraints: ");
		result.append(constraints);
		result.append(", name: ");
		result.append(name);
		result.append(')');
		return result.toString();
	}

}
